package com.bigdata.es;

import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.support.AbstractClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PuaEs {

    private static TransportClient getClient() throws UnknownHostException {
        Settings settings = Settings.builder().put("cluster.name", "hyf-es-cluster").build();
        TransportClient client = new PreBuiltTransportClient(settings);
        TransportAddress transportAddress = new TransportAddress(InetAddress.getByName("192.168.1.86"), 9300);
        client = client.addTransportAddress(transportAddress );
        return client;
    }

    private static void showNodesInfo(TransportClient client) {
        client.connectedNodes();
        for (DiscoveryNode discoveryNode: client.listedNodes()) {
            System.out.println(discoveryNode.getAddress());
        }
    }

    public static void main(String[] args) throws UnknownHostException {
        TransportClient client = getClient();
        //client.prepareIndex("bitch", "nani").get();
        List<DiscoveryNode> nodes = client.connectedNodes();
        System.out.println(nodes);
//        Map<String,Object> map = new HashMap<>();
//        map.put("passwd", "eda");
//        client.prepareIndex("hehe", "mytype", "1").setSource(map).get();

        GetRequestBuilder grb = client.prepareGet("hehe", "mytype", "1");
        GetResponse getResponse = grb.get();

        System.out.println(getResponse.getSourceAsString());


//        GetResponse getResponse = client.prepareGet("hehe", "mytype", "1").get();
//
//        getResponse.getSourceAsMap();
//        System.out.println(getResponse.getSourceAsString());
    }
}
